#! /bin/sh

#---------------------------------------------------------------------
#                            make-app-file                            
# 
# Creates the Erlang application file by scanning the source directory.
#---------------------------------------------------------------------

#---------------------------------------------------------------------
#                           chicken and egg                           
#---------------------------------------------------------------------

which perl >/dev/null 2>/dev/null || {
  echo "erlang-rebar/make-app-file: fatal: can't locate perl" 1>&2
  exit 1
}

if test -z "${FW_ROOT}"
  then
    FW_ROOT=`echo "$0" | perl -pe 's%fw/template/erlang-rebar/make-app-file$%%;'`
    export FW_ROOT
  fi

test -f "${FW_ROOT}/share/fw/sh/fw-find.sh" || {
  echo "erlang-rebar/make-app-file: fatal: can't determine FW_ROOT" 1>&2
  echo "erlang-rebar/make-app-file: please set the environment variable" 1>&2
  exit 1
}

. "${FW_ROOT}/share/fw/sh/fw-find.sh"

#---------------------------------------------------------------------
#                              routines                               
#---------------------------------------------------------------------

fw_source "erlang-rebar/make-app-file" "sh/fw-exec.sh"
fw_source "erlang-rebar/make-app-file" "sh/parse-args.sh"
fw_source "erlang-rebar/make-app-file" "sh/validate-args.sh"

#---------------------------------------------------------------------
#                                main                                 
#---------------------------------------------------------------------

parse_args "erlang-rebar/make-app-file" "$@"

validate_args "erlang-rebar/make-app-file" "top" ""

cd "$top" || exit 1

if test -d fw/bin
  then
    PATH="`pwd`/fw/bin:$PATH"
    export PATH
  fi

eval `fw_exec "template/erlang-rebar/load-config"` 

test $? = 0 || exit 1

fw_find template/erlang-rebar/fwte-makeappfile fwte_makeappfile

test -n "$fwte_makeappfile" || fatal "template/erlang-rebar/make-app-file: missing fwte-makeappfile"

if test -f "ebin/${FW_ERL_APP_NAME}.app" ; then
  echo "app file already exists in ebin dir"
  exit 0
fi

if test -f "src/${FW_ERL_APP_NAME}.app.src" ; then
  echo "app file already exists in src dir"
  exit 0
fi

tmpdir=make_app_file_$$
rm -rf "$tmpdir"

if test -n "$FW_ERL_APP_MODULES" 
  then
    modules="--modules \"\$FW_ERL_APP_MODULES\""
  else
    modules=""
  fi

if test -n "$FW_ERL_APP_REGISTERED" 
  then
    registered="--registered \"\$FW_ERL_APP_REGISTERED\""
  else
    registered=""
  fi

FW_ERL_APP_START_ARGS=${FW_ERL_APP_START_ARGS-"[]"}

if test -n "$FW_ERL_APP_START_MODULE"
  then
    premodline="{ mod, { $FW_ERL_APP_START_MODULE, $FW_ERL_APP_START_ARGS } }"
  else
    premodline=""
  fi

if test -n "${FW_ERL_APP_MOD_LINE-$premodline}"
  then
    modline="--mod \"\${FW_ERL_APP_MOD_LINE-$premodline}\""
  else
    modline=""
  fi

FW_ERL_APP_ENVIRONMENT=${FW_ERL_APP_ENVIRONMENT-"[]"}

if test -n "$FW_ERL_APP_EXTRA"
  then
    extra="--extra \"\$FW_ERL_APP_EXTRA\""
  else
    extra=""
fi

if test -n "$FW_ERL_PREREQ_APPLICATIONS_EXTRA"
  then
    prereqs="--prereqs \"\$FW_ERL_PREREQ_APPLICATIONS_EXTRA\""
  else
    prereqs=""
  fi

eval \"\$fwte_makeappfile\"                                             \
                    --application \"\${FW_ERL_APP_NAME}\"               \
                    --description \"\${FW_PACKAGE_SHORT_DESCRIPTION}\"  \
                    --version \"\${FW_PACKAGE_VERSION}\"                \
                    $modules                                            \
                    $registered                                         \
                    $prereqs                                            \
                    $modline                                            \
                    --env \"\${FW_ERL_APP_ENVIRONMENT}\"                  \
                    $extra                                              \
                    src/*.erl > "src/${FW_ERL_APP_NAME}.app.src"
